# Downloads and installs the latest SFML snapshot
cmake_minimum_required(VERSION 2.6)

# Add dependency for ExternalProject
include(ExternalProject)

# Download and compile SFML using an external project
ExternalProject_Add(physfs_EXTERNAL
    CMAKE_ARGS
        -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
        -DPHYSFS_BUILD_WX_TEST=false
        -DPHYSFS_INTERNAL_ZLIB=true
        -DPHYSFS_BUILD_STATIC=true
        -DPHYSFS_BUILD_SHARED=false
        -Wno-dev
    DOWNLOAD_COMMAND ""
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/physfs-source
)

# Recover project paths for additional settings
ExternalProject_Get_Property(physfs_EXTERNAL SOURCE_DIR BINARY_DIR INSTALL_DIR )

# Tell cmake that the external project generated a library so we
# can add dependencies here instead of later
add_library(physfs UNKNOWN IMPORTED)
set_property(TARGET physfs
  PROPERTY IMPORTED_LOCATION
  ${INSTALL_DIR}/lib/libphysfs.a
  )
add_dependencies(physfs physfs_EXTERNAL)

# Set properties as per FindModules
set(physfs_FOUND 
  TRUE
  CACHE INTERNAL "" FORCE 
)

set(physfs_INCLUDE_DIRS 
  "${INSTALL_DIR}/include"
  CACHE INTERNAL "" FORCE
)

set(physfs_LIBRARIES
  "${INSTALL_DIR}/lib/libphysfs.a"
  CACHE INTERNAL "" FORCE
)

set(physfs_DEFINITIONS
  " -fno-common "
  CACHE INTERNAL "" FORCE
)

# On Apple platforms, we also need to include GUI frameworks
if( __APPLE__ )
  list(APPEND physfs_LIBRARIES "-framework Carbon" "-framework IOKit")
endif()
